package com.youlu.campus.web.share;

import com.youlu.campus.entity.QQMiniUserPO;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.UserPointsDetail;
import com.youlu.campus.service.platform.PointPlatformConfigService;
import com.youlu.campus.service.ranking.UserPointRankingService;
import com.youlu.campus.service.share.QQMiniProgramShareRecordService;
import com.youlu.campus.service.share.QQMiniProgramShareService;
import com.youlu.campus.service.visitor.UserPointsService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

@Slf4j
@RestController
public class QQMiniProgramShareController {
    @Autowired
    private QQMiniProgramShareService qQMiniProgramShareService;
    @Autowired
    private QQMiniProgramShareRecordService qQMiniProgramShareRecordService;
    @Autowired
    private PointPlatformConfigService pointPlatformConfigService;
    @Autowired
    private UserPointsService userPointsService;
    @Autowired
    private UserPointRankingService userPointRankingService;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 获取平台分享信息
     *
     * @param appId
     * @return
     */
    @GetMapping("/mini-app/share/qq/platform")
    public QResult getQQMiniPlatform(@RequestParam("appId") String appId) {
        return QResult.success(qQMiniProgramShareService.getPlatformShareInfo(appId, RequestContext.getUserId()));

    }

    /**
     * 获取活动分享信息
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/share/qq/activity")
    public QResult getQQMiniActivity(@RequestParam("activityId") String activityId) {
        return QResult.success(qQMiniProgramShareService.getActivityShareInfo(RequestContext.getAppId(),
                activityId, RequestContext.getUserId()));

    }

    @GetMapping("/mini-app/share/qq/create")
    public QResult getQQMiniPlatform(@RequestParam("appId") String appId, @RequestParam("sharedByUserId") String sharedByUserId) {
        log.info(":>>> 平台级别分享小程序appId:{},sharedByUserId:{}", appId, sharedByUserId);
        boolean re = qQMiniProgramShareRecordService.createInviteRecord(appId, sharedByUserId, RequestContext.getUserId());
        if (re) {
            // TODO:添加积分
            Integer point = pointPlatformConfigService.getPoint(appId, "invite");
            UserPointsDetail detail = new UserPointsDetail();
            detail.setUserId(sharedByUserId);
            detail.setAction("add");
            detail.setBizDesc("邀请用户");
            detail.setBizType("invite");
            detail.setOptDate(new Date());
            detail.setPoints(point);
            detail.setComments("邀请用户");
            userPointsService.addPoint(detail);
            Query query = new Query().addCriteria(Criteria.where("accountInfoId").is(sharedByUserId).and("appId").is(appId));
            QQMiniUserPO userPO = mongoTemplate.findOne(query, QQMiniUserPO.class);
            if (Objects.nonNull(userPO)) {
                UserPO u = new UserPO();
                BeanUtils.copyProperties(userPO, u);
                u.setId(userPO.getAccountInfoId());
                userPointRankingService.updatePoint(u, "", point);
            }
        }
        return QResult.success(re);
    }
}
